package me.zhulu.dubbo.pagination2;

import java.io.Serializable;

/**
 * 分页信息
 * 
 * @author amyson wu
 * 
 */
public class PageInfo implements Serializable {
	
  /**
	 * 
	 */
	private static final long serialVersionUID = -4185629002692162583L;

// 第一页
  public static final int PAGE_FIRST = 1;
  
  public static final int SIZE_NONE_PAGED = Integer.MAX_VALUE - 100;

  public static final int DEFAULT_PAGE_SIZE = 15;

  public static PageInfo NO_PAGED = new PageInfo(SIZE_NONE_PAGED, 1);

  public static PageInfo ONE_ROW = new PageInfo(1, 1);

  // 页面大小
  private int pageSize = DEFAULT_PAGE_SIZE;

  // 页面位置
  private int pageIndex = 1;

  private boolean rowStraight = false;// 是否直接指定了起始与结束行号

  private int firstRow = 0, lastRow = 0;// 另外一种结束方式:直接指定开始和结束行号

  private int offset; // 第一个返回记录行的偏移量, 初始记录行的偏移量是 0(而不是 1)
  private int rows; // 返回记录行的最大数目

  private int totalCount; // total records count
  private int pageCount; // total page count

  public PageInfo() {
  }

  public PageInfo(int pageSize) {
    this.pageSize = pageSize;
  }

  public PageInfo(int pageSize, int pageIndex) {
    this(pageSize, pageIndex, false);
  }

  public PageInfo(int pageSizeOrFirstRow, int pageIndexOrLastRow, boolean rowStraight) {
    if (rowStraight) {
      this.firstRow = pageSizeOrFirstRow;
      this.lastRow = pageIndexOrLastRow;
      this.rowStraight = true;
      this.pageSize = this.lastRow - this.firstRow + 1;
    } else {
      this.pageSize = pageSizeOrFirstRow;
      this.pageIndex = pageIndexOrLastRow;
    }
  }

  public int getPageIndex() {
    return pageIndex;
  }

  public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getFirstRow() {
    if (rowStraight) {
      return firstRow;
    }
    
    return (pageIndex - 1) * pageSize + 1;
  }

  public int getLastRow() {
    if (rowStraight) {
      return lastRow;
    }
    
    return pageSize * pageIndex;
  }

  public int getOffset() {
    if (rowStraight) {
      offset = firstRow;
    } else {
      offset =  (pageIndex - 1) * pageSize;
    }

    return offset < 0 ? 0 : offset;
  }

  public int getRows() {
    rows = pageSize;
    return rows;
  }

  public int getTotalCount() {
    return totalCount;
  }

  public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
    this.pageCount = calcPagesCount();
  }

  public int getPageCount() {
    return pageCount;
  }

  protected int calcPagesCount() {
    if (totalCount == 0) {
      return 0;
    } else {
      int base = totalCount / getPageSize();
      int rest = totalCount % getPageSize();
      if (rest > 0) {
        return base + 1;
      } else {
        return base;
      }
    }
  }

  public static void main(String[] args) {
    PageInfo pi = NO_PAGED;
    int firstRow = pi.getFirstRow() > 0 ? pi.getFirstRow() - 1 : 0;
    System.out.println(pi.getLastRow() - firstRow + 1);
  }

    @Override
    public String toString() {
        return "PageInfo{" +
                "pageSize=" + pageSize +
                ", pageIndex=" + pageIndex +
                ", rowStraight=" + rowStraight +
                ", firstRow=" + firstRow +
                ", lastRow=" + lastRow +
                ", offset=" + offset +
                ", rows=" + rows +
                ", totalCount=" + totalCount +
                ", pageCount=" + pageCount +
                '}';
    }
}
